package cn.com.coding.app;

import cn.com.coding.common.lang.Const;
import cn.com.coding.common.lang.ResT;
import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.system.domin.ProductCategoryEntity;
import cn.com.coding.system.service.ProductCategoryService;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 景点分类
 *
 * @author mxz
 * @email codingce@gmail.com
 * @date 2022-03-05 14:01:23C
 */
@RestController
@RequestMapping("cat/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 列表
     */
    @RequestMapping("/categoryList")
    public ResT list(@RequestBody Map<String, Object> params) {
        PageUtil page = productCategoryService.queryPage(params);

        return ResT.ok().put("productCategory", page);
    }

    /**
     * 专题
     */
    @RequestMapping("/getCategory/{category_status}")
    public ResT all(@PathVariable("category_status") int categoryStatus) {

        List<ProductCategoryEntity> list = categoryStatus == 2 ? productCategoryService.list(new QueryWrapper<ProductCategoryEntity>()
                .eq("category_status", categoryStatus).last("limit 8")) : productCategoryService.list(new QueryWrapper<ProductCategoryEntity>()
                .eq("category_status", categoryStatus)) ;
        return ResT.ok().setData(MapUtil.builder().put("categoryList", list).build());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{pid}")
    public ResT info(@PathVariable("pid") String pid) {
        ProductCategoryEntity productCategory = productCategoryService.getById(pid);

        return ResT.ok().put("productCategory", productCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResT save(@RequestBody ProductCategoryEntity productCategory) {
        productCategoryService.save(productCategory);

        return ResT.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResT update(@RequestBody ProductCategoryEntity productCategory) {
        productCategoryService.updateById(productCategory);

        return ResT.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResT delete(@RequestBody String[] pids) {
        productCategoryService.removeByIds(Arrays.asList(pids));

        return ResT.ok();
    }

}
